﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.CampaignSystem.Issues.IssueQuestTasks.CaptureAndBringNpcTask
// Assembly: TaleWorlds.CampaignSystem, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E85F8C15-4DF6-4E9C-A58A-29177E40D07A
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\bin\Win64_Shipping_Client\TaleWorlds.CampaignSystem.dll

using System;
using System.Collections.Generic;
using TaleWorlds.CampaignSystem.Actions;
using TaleWorlds.CampaignSystem.Conversation;
using TaleWorlds.CampaignSystem.MapEvents;
using TaleWorlds.CampaignSystem.Party;
using TaleWorlds.SaveSystem;

#nullable disable
namespace TaleWorlds.CampaignSystem.Issues.IssueQuestTasks
{
  public class CaptureAndBringNpcTask : QuestTaskBase
  {
    [SaveableField(10)]
    private Hero _targetHero;
    public bool isFinishedSuccess;

    internal static void AutoGeneratedStaticCollectObjectsCaptureAndBringNpcTask(
      object o,
      List<object> collectedObjects)
    {
      ((QuestTaskBase) o).AutoGeneratedInstanceCollectObjects(collectedObjects);
    }

    protected override void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
    {
      base.AutoGeneratedInstanceCollectObjects(collectedObjects);
      collectedObjects.Add((object) this._targetHero);
    }

    internal static object AutoGeneratedGetMemberValue_targetHero(object o)
    {
      return (object) ((CaptureAndBringNpcTask) o)._targetHero;
    }

    public CaptureAndBringNpcTask(
      Hero targetHero,
      Action onSucceededAction,
      Action onFailedAction,
      Action onCanceledAction,
      DialogFlow dialogFlow = null)
      : base(dialogFlow, onSucceededAction, onFailedAction, onCanceledAction)
    {
      this._targetHero = targetHero;
    }

    private DialogFlow GetTakePrisonerDialogFlow()
    {
      return DialogFlow.CreateDialogFlow("lord_start", int.MaxValue).NpcLine("{=2vcfifbb}I yield").Condition((ConversationSentence.OnConditionDelegate) (() => Hero.OneToOneConversationHero == this._targetHero && this.isFinishedSuccess)).BeginPlayerOptions().PlayerOption("{=cbzJRaDJ}You are my prisoner now!").GotoDialogState("lord_start").Consequence((ConversationSentence.OnConsequenceDelegate) (() => TakePrisonerAction.Apply(Hero.MainHero.PartyBelongedTo.Party, this._targetHero))).EndPlayerOptions();
    }

    public void OnPlayerBattleEnd(MapEvent mapEvent)
    {
      if (PartyBase.MainParty.Side != mapEvent.WinningSide)
        return;
      foreach (PartyBase involvedParty in mapEvent.InvolvedParties)
      {
        if (involvedParty.Side == mapEvent.DefeatedSide)
        {
          foreach (CharacterObject prisonerHero in PartyBase.MainParty.PrisonerHeroes)
          {
            if (prisonerHero == this._targetHero.CharacterObject)
            {
              this.Finish(QuestTaskBase.FinishStates.Success);
              break;
            }
          }
        }
        if (!this.IsActive)
          break;
      }
    }

    protected override void OnFinished() => this._targetHero = (Hero) null;

    public override void SetReferences()
    {
      CampaignEvents.OnPlayerBattleEndEvent.AddNonSerializedListener((object) this, new Action<MapEvent>(this.OnPlayerBattleEnd));
    }
  }
}
